﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ВУИ
{
    public partial class Pasport : Form
    {
        int id;
        Form1 Main;
        public Pasport(int ID, Form1 MAIN)
        {
            InitializeComponent();
            id = ID;
            Main = MAIN;

            if (Main.lPatient[Main.itnPatietn(id)].This == null) Main.lPatient[Main.itnPatietn(id)].This = new SNP();
            if (Main.lPatient[Main.itnPatietn(id)].Mother == null) Main.lPatient[Main.itnPatietn(id)].Mother = new SNP();
            if (Main.lPatient[Main.itnPatietn(id)].Father == null) Main.lPatient[Main.itnPatietn(id)].Father = new SNP();

            textBox1.Text = Main.lPatient[Main.itnPatietn(id)].This.Surname;
            textBox2.Text = Main.lPatient[Main.itnPatietn(id)].This.Name;
            textBox3.Text = Main.lPatient[Main.itnPatietn(id)].This.Patronymic;

            textBox6.Text = Main.lPatient[Main.itnPatietn(id)].Mother.Surname;
            textBox5.Text = Main.lPatient[Main.itnPatietn(id)].Mother.Name;
            textBox4.Text = Main.lPatient[Main.itnPatietn(id)].Mother.Patronymic;

            textBox9.Text = Main.lPatient[Main.itnPatietn(id)].Father.Surname;
            textBox8.Text = Main.lPatient[Main.itnPatietn(id)].Father.Name;
            textBox7.Text = Main.lPatient[Main.itnPatietn(id)].Father.Patronymic;

            dateTimePicker1.Value = Main.lPatient[Main.itnPatietn(id)].Date_of_birth;
            dateTimePicker2.Value = Main.lPatient[Main.itnPatietn(id)].Statement_date;

            textBox11.Text = Main.lPatient[Main.itnPatietn(id)].Adress;
            textBox12.Text = Main.lPatient[Main.itnPatietn(id)].Tel;
            textBox13.Text = Convert.ToString(Main.lPatient[Main.itnPatietn(id)].Growth);
            textBox14.Text = Convert.ToString(Main.lPatient[Main.itnPatietn(id)].Weight);
            textBox15.Text = Convert.ToString(Main.lPatient[Main.itnPatietn(id)].Gestation);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Main.lPatient[Main.itnPatietn(id)].This.Surname = textBox1.Text;
            Main.lPatient[Main.itnPatietn(id)].This.Name = textBox2.Text;
            Main.lPatient[Main.itnPatietn(id)].This.Patronymic = textBox3.Text;

            Main.lPatient[Main.itnPatietn(id)].Mother.Surname = textBox6.Text;
            Main.lPatient[Main.itnPatietn(id)].Mother.Name = textBox5.Text;
            Main.lPatient[Main.itnPatietn(id)].Mother.Patronymic = textBox4.Text;

            Main.lPatient[Main.itnPatietn(id)].Father.Surname = textBox9.Text;
            Main.lPatient[Main.itnPatietn(id)].Father.Name = textBox8.Text;
            Main.lPatient[Main.itnPatietn(id)].Father.Patronymic = textBox7.Text;

            Main.lPatient[Main.itnPatietn(id)].Date_of_birth = dateTimePicker1.Value;
            Main.lPatient[Main.itnPatietn(id)].Statement_date = dateTimePicker2.Value;

            Main.lPatient[Main.itnPatietn(id)].Adress = textBox11.Text;
            Main.lPatient[Main.itnPatietn(id)].Tel = textBox12.Text;
            Main.lPatient[Main.itnPatietn(id)].Growth = Convert.ToInt32(textBox13.Text);
            Main.lPatient[Main.itnPatietn(id)].Weight = Convert.ToInt32(textBox14.Text);
            Main.lPatient[Main.itnPatietn(id)].Gestation = Convert.ToInt32(textBox15.Text);
            Close();
            Main.refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e){


            int d = (dateTimePicker2.Value - dateTimePicker1.Value).Days;
            int m = 0;
            int y = 0;
            int n = 0;
            while (d > 365)
            {
                d -= 365;
                y++;
            }
            while (d > 30)
            {
                d -= 30;
                m++;
            }
            if ((y < 1) & (m >= 1))
                while (d > 7)
                {
                    d -= 7;
                    n++;
                }
            string s = "";
            if (y > 0)
            {
                s = Convert.ToString(y) + "год(лет) ";
                if (m > 0) s += Convert.ToString(m) + "месяц(а)";
            }
            else
            {
                if (m >= 1)
                {
                    s = Convert.ToString(m) + "месяц(а) ";
                    if (n > 0) s += Convert.ToString(n) + "недели";
                }
                else
                {
                    s = Convert.ToString(d) + "дней(я) ";
                }
            }
         
            textBox10.Text = s;
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            int d = (dateTimePicker2.Value - dateTimePicker1.Value).Days;
            int m = 0;
            int y = 0;
            int n = 0;
            while (d > 365)
            {
                d -= 365;
                y++;
            }
            while (d > 30)
            {
                d -= 30;
                m++;
            }
            if ((y < 1) & (m>=1))
                while (d > 7)
                {
                    d -= 7;
                    n++;
                }
            string s = "";
            if (y > 0)
            {
                s = Convert.ToString(y) + "год(лет) ";
                if (m>0) s+=Convert.ToString(m) + "месяц(а)";
            }
            else
            {
                if (m >= 1)
                {
                    s = Convert.ToString(m) + "месяц(а) ";
                    if (n>0) s+=Convert.ToString(n) + "недели";
                }
                else
                {
                    s = Convert.ToString(d) + "дней(я) ";
                }
            }

            textBox10.Text = s;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            PaspOt p = new PaspOt(Main,id);
            p.ShowDialog();

        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
